<?php

namespace Lib;

class WxHelper
{
    /**
     * 将本站某URL作为回调得到对应的微信授权链接
     * 
     * @param string $route
     * @param string $scope 'base'|'userinfo'
     * @return string
     */
    public static function getAuthUrl($route, $scope)
    {
        if (!in_array($scope, ['base', 'userinfo'])) {
            die('scope must be base or userinfo');
        }
        $app = \Slim\Slim::getInstance();
        
        $baseUrl = $app->config('base_url') ? $app->config('base_url') : $app->request->getUrl();
        $redirectUri = urlencode($baseUrl . $app->urlFor($route) . ($app->request->get('uuid') ? '?uuid=' . $app->request->get('uuid') : ''));
        $wxscope = $scope == 'base' ? 'snsapi_base' : 'snsapi_userinfo';
        $appId = $app->config('wx_appid');
        $authUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appId .'&redirect_uri=' . $redirectUri . '&response_type=code&scope=' . $wxscope . '&state=1#wechat_redirect';
        return $authUrl;
    }
    
    /**
     * 获取微信用户信息
     * 
     * @param string $route
     * @param string $scope 'base'|'userinfo'
     * @return array|string|boolean
     */
    public static function getUserInfo($code, $scope)
    {
        if (!in_array($scope, ['base', 'userinfo'])) {
            die('scope must be base or userinfo');
        }
        
        if (isset($_SERVER['HTTP_HOST']) && ($_SERVER['HTTP_HOST'] == 'localhost' || $_SERVER['HTTP_HOST'] == '10.0.3.2')) {
            return array(
                'nick' => uniqid('u_'),
                'head' => 'http://baidu.com',
                'openid' => uniqid('wx_')
            );
        }
        
        $app = \Slim\Slim::getInstance();
        
        $appId     = $app->config('wx_appid');
        $appSecret = $app->config('wx_secret');
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?';
        $url .= 'appid=' . $appId . '&secret=' . $appSecret;
        $url .= '&code=' . $code . '&grant_type=authorization_code';
        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, $url); 
        curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE);
        $ret = curl_exec($c);
        $retArr = json_decode($ret, true);
        curl_close($c);
        
        if ($scope == 'base') {
            return isset($retArr['openid']) ? $retArr['openid'] : false;
        } else if (!isset($retArr['access_token']) || !isset($retArr['openid'])) {
            return false;
        }
        
        $accessToken = $retArr['access_token'];
        $openid = $retArr['openid'];
        $url2 = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $accessToken . '&openid=' . $openid . '&lang=zh_CN';
        $c2 = curl_init();
        curl_setopt($c2, CURLOPT_URL, $url2); 
        curl_setopt($c2, CURLOPT_RETURNTRANSFER, TRUE);
        $ret2 = curl_exec($c2);
        $retArr2 = json_decode($ret2, true);
        curl_close($c2);
        
        return array(
            'nick' => $retArr2['nickname'],
            'head' => $retArr2['headimgurl'],
            'openid' => $openid
        );
    }
}